﻿using System;
using OneBusAway.Util;

namespace OneBusAway.Entities
{
    public class ArrivalAndDeparture
    {
        private static DateTime unixEpoch = new DateTime(1970, 1, 1);

        [FromXml("routeId")]
        public string RouteId { get; set; }

        [FromXml("tripId")]
        public string TripId { get; set; }

        [FromXml("routeShortName")]
        public string RouteShortName { get; set; }

        [FromXml("tripHeadsign")]
        public string TripHeadsign { get; set; }

        [FromXml("predictedArrivalTime", Converter = "MsToDateTime")]
        public DateTime PredictedArrivalTime { get; set; }

        [FromXml("scheduledArrivalTime", Converter = "MsToDateTime")]
        public DateTime ScheduledArrivalTime { get; set; }

        [FromXml("predictedDepartureTime", Converter = "MsToDateTime")]
        public DateTime PredictedDepartureTime { get; set; }

        [FromXml("scheduledDepartureTime", Converter = "MsToDateTime")]
        public DateTime ScheduledDepartureTime { get; set; }

        public int MinutesUntilDeparture
        {
            get
            {
                return (int)DateTime.Now.Subtract(this.PredictedDepartureTime).TotalMinutes;
            }
        }

        public static DateTime MsToDateTime(string msFromUnixEpoch)
        {
            return unixEpoch.AddMilliseconds(Int64.Parse(msFromUnixEpoch)).ToLocalTime();
        }
    }
}
